//
//  CZRefresh.swift
//  WeiBo
//
//  Created by Apple on 16/7/1.
//  Copyright © 2016年 cx. All rights reserved.
//

import UIKit
//刷新控件的状态枚举
enum CZRefreshType: Int{
//    正常
    case Normal = 0
//    下拉
    case Pulling = 1
//    刷新
    case Refreshing = 2
}
let CZRefreshHeight: CGFloat = 50

class CZRefresh: UIControl {

//    被观察者
    var scrollView: UIScrollView?
    
    var refreshState:CZRefreshType = .Normal{
        didSet{
            switch refreshState{
            case .Normal:
                messageLabel.text = "正常"
                if oldValue == .Refreshing{
                    UIView.animateWithDuration(0.25, animations: { () -> Void in
                        self.scrollView?.contentInset.top -= CZRefreshHeight
                        
                    })
                }
            case .Pulling:
                messageLabel.text = "下拉中"
            case .Refreshing:
                messageLabel.text = "刷新中"
                UIView.animateWithDuration(0.25, animations: { () -> Void in
                    self.scrollView?.contentInset.top += CZRefreshHeight
                    }, completion: { (_) -> Void in
                        self.sendActionsForControlEvents(UIControlEvents.ValueChanged)
                })
                
            }
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: CGRect(x: 0, y: -CZRefreshHeight, width: SCREENW, height: CZRefreshHeight))
        
        setupUI()
    }
//    外界调用
    func endRefreshing(){
        refreshState = .Normal
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
//    设置视图
    private func setupUI(){
        backgroundColor = UIColor.orangeColor()
        addSubview(messageLabel)
    }
    override func layoutSubviews() {
        super.layoutSubviews()
        messageLabel.frame = CGRect(x: 0, y: 0, width: SCREENW, height: CZRefreshHeight)
    }
    override func willMoveToSuperview(newSuperview: UIView?) {
        guard let scrollView = newSuperview as? UIScrollView
            else{
                return
        }
        self.scrollView = scrollView
        //        注册观察者
        scrollView.addObserver(self, forKeyPath: "contentOffset", options: NSKeyValueObservingOptions.New, context: nil)
        
    }
    //    观察者中实现
    override func observeValueForKeyPath(keyPath: String?, ofObject object: AnyObject?, change: [String : AnyObject]?, context: UnsafeMutablePointer<Void>) {
        
        let contentOffSetY = self.scrollView!.contentOffset.y
        // 判断scrollView 是否在拖动
        let contentInsetTop = self.scrollView!.contentInset.top
        print(contentInsetTop)
        // 代表拖动中
        if scrollView!.dragging{
            if contentOffSetY > -contentInsetTop - CZRefreshHeight  && refreshState == .Pulling{
                print("正常")
                refreshState = .Normal
            }else if contentOffSetY <=  -contentInsetTop - CZRefreshHeight && refreshState == .Normal {
                print("下拉中")
                refreshState = .Pulling
            }
            
        }else {
            //        松手
            if refreshState == .Pulling{
                refreshState = .Refreshing
            }
            
            
        }
    }
//    加载控件
    private lazy var messageLabel: UILabel = {
        let lab = UILabel()
        lab.font = UIFont.systemFontOfSize(15)
        lab.textColor = UIColor.whiteColor()
        lab.textAlignment = .Center
        lab.text = "正常"
        return lab
    
    }()

    deinit{
        self.scrollView?.removeObserver(self, forKeyPath: "contentOffset")
    }

}
